﻿@{
    ViewBag.Title = "Manage View (/v)";
}
<script>
    var viewname = '@ViewData["name"]';

    $(document).ready(function () {
        if (contentname.length !== 0) {
            // Show loading animation
            getData('/v/' + viewname, '', loadData, false, '', $('#grid-container .grid-items-list'), viewname);
        }
        else {
            $('#grid-container').epicGrid({ contenttype: viewname });
        }
    });

    var grid_item_html = '<div class="grid-item grid-item-bar">' +
                    '<div class="left-padding">' +
                    '</div>' +
                    '<div class="checkbox">' +
                    '    <input type="checkbox" class="checkbox" /></div>' +
                    '<div class="grid-item-content-area">' +
                    '<div class="grid-item-left-tag-area">' +
                    '    <div class="id-tag">' +
                    '        ##0##</div>' +
                    '</div>' +
                    '<div class="grid-item-title-area">' +
                    '    ##2##</div>' +
                    '<div class="grid-item-date-area">' +
                    '    ##1##</div>' +
                    '</div></div>';

    function loadData(data, innercontainer) {
        if (data.error)
            return;

        innercontainer.html("");
        // Show loading animation

        $.each(data, function (i, item) {
            var item_html = grid_item_html;
            var count = 0;
            for (var key in item) {
                item_html = item_html.replace('##' + count++ + '##', item[key]);
            }
            innercontainer.append(item_html);
        });

        $('#grid-container').epicGrid({ contenttype: viewname, data: data });

        $('.add-button').bind('click', addCreatorItem);
        $('.edit-button').bind('click', editItems);
    }

    function addCreatorItem() {
        var newDivId = "new" + viewname + $('#editor').find('div.add-div').length;
        $('#editor').append('<div class="add-div"></div>');
        $.get("/Create/v/" + viewname + "?embedded=true&target=" + newDivId, function (html) {
            $('#editor .add-div:last').html(html);
        });
    }

    function editItems() {
        var selectedItems = $('.checked-grid-item-bar');
        $.each(selectedItems, function (i, item) {
            var itemID = $(item).find('.id-tag').html().trim();
            addEditorItem(itemID);
        });
    }

    function addEditorItem(id) {
        var newDivId = "edit" + contentname + id;
        if ($('#' + newDivId).length != 0)
            return;
        $('#editor').append('<div class="edit-div ' + newDivId +'"></div>');
        $('#editor div.edit-div.' + newDivId).load("/Edit/v/" + viewname + "/" + id + "/?embedded=true&loadscripts=false&target=" + newDivId);
    }
</script>

